package com.ls.hnks.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.ls.hnks.util.Result;
import com.ls.hnks.util.ResultCodeEnum;
import com.ls.hnks.common.constant.RoleConstant;
import com.ls.hnks.model.Todo;
import com.ls.hnks.model.User;
import com.ls.hnks.service.TodoService;
import com.ls.hnks.service.UserService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@SaCheckLogin
@RestController
@RequestMapping("/todo")
public class ToDoController {

    @Autowired
    private UserService userService;

    @Autowired
    private TodoService todoService;

    @RequestMapping("/byUser")
    public Result getPageByUser(
      @ApiParam("客户ID") @RequestParam(value = "userId") String userId,
                                @ApiParam("分页查询的页码数") @RequestParam(value = "page",defaultValue = "1")Integer page,
                                @ApiParam("分页查询的页大小") @RequestParam(value = "size",defaultValue = "20")Integer size) {
        String selfId = StpUtil.getLoginId().toString();
        User self = userService.getById(selfId);
        User other = userService.getById(userId);
        if (other == null) {
            return Result.error(ResultCodeEnum.USER_NOT_FOUND);
        }
        // 管理员可以查看所有用户的待办,普通用户可以查看同组用户的待办
        if (self.getRight() != RoleConstant.ADMIN && !Objects.equals(self.getGroupid(), other.getGroupid())) {
            return Result.error(ResultCodeEnum.FORBIDDEN);
        }
        return todoService.getPageByUserId(userId,page,size);
    }

    @RequestMapping("/byUserAndDate")
    public Result getListByUserAndDate(String userId,String date) {
        String selfId = StpUtil.getLoginId().toString();
        User self = userService.getById(selfId);
        User other = userService.getById(userId);
        if (other == null) {
            return Result.error(ResultCodeEnum.USER_NOT_FOUND);
        }
        // 管理员可以查看所有用户的行程,普通用户可以查看同组用户的行程
        if (self.getRight() != RoleConstant.ADMIN && !Objects.equals(self.getGroupid(), other.getGroupid())) {
            return Result.error(ResultCodeEnum.FORBIDDEN);
        }
        return todoService.getListByUserAndDate(userId, date);
    }

    @RequestMapping("/details/{id}")
    public Result getDetails(@PathVariable String id) {
        Todo todo = todoService.getById(id);
        if (todo == null){
            return Result.error(ResultCodeEnum.TODO_NOT_FOUND);
        }
        return Result.ok(todo);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Todo todo) {
        int createUserId = Integer.parseInt(StpUtil.getLoginId().toString());
        User byId = userService.getById(createUserId);
        if(byId.getRight() != RoleConstant.ADMIN && todo.getExecutorid() != createUserId){
            return Result.fail().message("非管理员用户无法指定执行人");
        }
        todo.setCreateuserid(createUserId);

        log.info("待办信息 --- {}", JSON.toJSONString(todo));
        todoService.saveOrUpdate(todo);
        return Result.ok();
    }

    @PostMapping("/remove/{id}")
    public Result remove( @PathVariable String id) {
        todoService.removeById(id);
        return Result.ok();
    }

}
